package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.LogAnnotation;
import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.dto.RecreationFileDto;
import com.csun.cmny.provider.dto.RecreationFileQueryDto;
import com.csun.cmny.provider.model.domain.OmcRecreationFile;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcRecreationFileService;
import com.csun.cmny.provider.validator.OmcRecreationFileAddValidator;
import com.csun.cmny.provider.validator.OmcRecreationFileEditValidator;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author Lijun
 * @description:
 * @date 2019/11/25 11:06
 */
@Slf4j
@RestController
@RequestMapping(value ="/recreationFile", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcRecreationFileController extends BaseController {
    @Resource
    private OmcRecreationFileService omcRecreationFileService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;
    /**
     * 添加娱乐文件
     * @param  fileDto
     * @return  the wrapper.
     */
    @ValidateAnnotation
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/addFile")
    public Wrapper addFile(@Validated(OmcRecreationFileAddValidator.class)
                               @RequestBody RecreationFileDto fileDto,BindingResult br){
        //获取机构ID，上传时间，创建人，创建人ID
        fileDto.setGroupId(getLoginAuthDto().getGroupId());
        Date date = new Date();
        fileDto.setCreatedTime(date);
        fileDto.setCreator(getLoginAuthDto().getUserName());
        fileDto.setCreatorId(getLoginAuthDto().getUserId());
        Integer result = save(fileDto);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

    /**
     * 修改娱乐文件
     * @param fileDto
     * @return
     */
    @ValidateAnnotation
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/editFile")
    public Wrapper editFile(@Validated(OmcRecreationFileEditValidator.class)
                           @RequestBody RecreationFileDto fileDto){
        Date date = new Date();
        fileDto.setCreatedTime(date);
        fileDto.setCreator(getLoginAuthDto().getUserName());
        fileDto.setCreatorId(getLoginAuthDto().getUserId());
        if(null == fileDto.getId()){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        Integer result = omcRecreationFileService.updateFile(fileDto);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

    /**
     * 删除娱乐文件
     * @param id
     * @return
     */
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/delectFileById/{id}", method = RequestMethod.POST)
    public Wrapper delectFileById(@PathVariable Long id){
        if(null == id){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        Integer result = omcRecreationFileService.deleteByKey(id);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

    /**
     * 分页查询娱乐文件
     * @param queryDto
     * @return
     */
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/queryFileWithPage", method = RequestMethod.POST)
    public Wrapper queryFileWithPage(@RequestBody RecreationFileQueryDto queryDto) {
        // 设置组织Id
        queryDto.setGroupId(getLoginAuthDto().getGroupId());
        PageInfo pageInfo = omcRecreationFileService.queryFileWithPage(queryDto);
        //return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),pageInfo);
    }

    /**
     * 娱乐文件保存
     * @param fileDto
     * @return
     */
    public Integer save(RecreationFileDto fileDto) {
        OmcRecreationFile omcRecreationFile = new OmcRecreationFile();
        BeanUtils.copyProperties(fileDto, omcRecreationFile);
        return omcRecreationFileService.save(omcRecreationFile);
    }
}
